package com.example.anni.zhuzhoujia.xiaowan;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.anni.zhuzhoujia.R;

import java.util.List;
import java.util.Map;

/**
 * Created by Anni on 2016/7/3.
 */
public class MyReadHistoryAdapter extends RecyclerView.Adapter<MyViewHold1> {
    private List<Map<String, String>> mDatas;
    private Context mContext;
    private LayoutInflater mInflater;
    protected int mCount = 0;

    public MyReadHistoryAdapter(Context mContext, List<Map<String, String>> mData) {
        this.mDatas = mData;
        this.mContext = mContext;
        mInflater = LayoutInflater.from(mContext);
    }

    //自定义接口
    public interface OnItemClickListener {
        void OnItemClick(View view, int position);

        void OnItemLongClick(View view, int position);
    }

    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;

    }

    public void setCount(int count) {
        mCount = count;
    }

    @Override
    public MyViewHold1 onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.read_history_adapter, parent, false);
        MyViewHold1 viewHold1 = new MyViewHold1(view);
        return viewHold1;
    }

    @Override
    public void onBindViewHolder(final MyViewHold1 holder, int position) {
        holder.tvReadHistoryTitle.setText(mDatas.get(position).get("title"));
        holder.tvReadHistoryDateline.setText(mDatas.get(position).get("dateline"));
        holder.tvReadHistoryViews.setText("阅读：" + mDatas.get(position).get("views"));

        //回调接口
        if (mOnItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int layoutPosition = holder.getLayoutPosition();
                    mOnItemClickListener.OnItemClick(holder.itemView, layoutPosition);
                }
            });
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    int layoutPosition = holder.getLayoutPosition();
                    mOnItemClickListener.OnItemLongClick(holder.itemView, layoutPosition);
                    return false;
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mCount;
    }

    public void removeRecycle(int position) {
        mDatas.remove(position);
        notifyItemRemoved(position);
        if (mDatas.size() == 0) {
            Toast.makeText(mContext, "已经没数据啦", Toast.LENGTH_SHORT).show();
        }
    }

}

class MyViewHold1 extends RecyclerView.ViewHolder {
    TextView tvReadHistoryTitle; // 标题
    TextView tvReadHistoryDateline;  // 时间
    TextView tvReadHistoryViews;  //  阅读数

    public MyViewHold1(View itemView) {
        super(itemView);
        tvReadHistoryTitle = (TextView) itemView.findViewById(R.id.tv_read_history_title);
        tvReadHistoryDateline = (TextView) itemView.findViewById(R.id.tv_read_history_dateline);
        tvReadHistoryViews = (TextView) itemView.findViewById(R.id.tv_read_history_views);
    }
}


